package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.service.CheckinService;
import com.zhentao.vo.CheckinInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 签到控制器
 */
@RestController
@RequestMapping("/api/checkin")
@CrossOrigin(origins = "*")
public class CheckinController {
    
    @Autowired
    private CheckinService checkinService;
    
    /**
     * 用户签到
     * @param userId 用户ID
     * @return 签到结果
     */
    @PostMapping("/sign")
    public Result<CheckinInfoVO> checkin(@RequestParam Long userId) {
        try {
            CheckinInfoVO vo = checkinService.checkin(userId);
            
            // 判断是否获得奖励
            String message = "签到成功！";
            if (vo.getContinuousDays() == 7) {
                message = "🎉 恭喜！获得1天VIP奖励！";
            } else if (vo.getContinuousDays() == 14) {
                message = "🎉 恭喜！获得3天VIP奖励！";
            } else if (vo.getContinuousDays() == 30) {
                message = "🎊 恭喜！获得7天VIP大礼包！";
            }
            
            return Result.success(message, vo);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("签到失败");
        }
    }
    
    /**
     * 获取用户签到信息
     * @param userId 用户ID
     * @return 签到信息
     */
    @GetMapping("/info")
    public Result<CheckinInfoVO> getCheckinInfo(@RequestParam Long userId) {
        try {
            CheckinInfoVO vo = checkinService.getCheckinInfo(userId);
            return Result.success(vo);
        } catch (Exception e) {
            return Result.error("获取签到信息失败");
        }
    }
}

